import {
	sub
} from '@/common/event.js'
import {
	getRqst
} from '@/common/request.js'
import store from '@/store/index.js'
export default {
	namespaced: true,
	state: {
		list: [],
		currentTime: 0,
		more:true
	},
	mutations: {
		removeInfo:(state,payload)=>{
			state.list = state.list.filter(e=>e.id != payload)
		},
		setListState: (state, payload) => {
			console.log(88599, payload)
			if (payload && payload.length > 0) {
				state.currentTime = payload[payload.length - 1].time
				state.list = state.list.concat(payload)
			}else{
				state.more = false
			}
		},
		setListStateInit: (state, payload) => {
			console.log(885991, payload)
			if (payload && payload.length > 0) {
				state.currentTime = payload[payload.length - 1].time
				state.list = payload
				state.more = true
			} else {
				state.currentTime = 0
				state.list = []
				state.more = false
			}
		},
	},
	actions: {
		async refreshList({
			commit,
			state
		}, {
			payload
		}) {
			if(state.more){
				const ret = await getRqst().get(`friend/friend/page`, {
					currentTime: state.currentTime
				})
				commit('setListState', ret.data)
			}
		},
		async refreshListForce({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`friend/friend/page`, {
				currentTime: 0
			})
			commit('setListStateInit', ret.data)
		},
	},
	init: function() {
		console.log(`执行朋友圈初始化`)
		store.dispatch({
			type: "friend/refreshList",
			payload: {}
		})
		sub(`loginSuccess`, function(e) {
			store.dispatch({
				type: "friend/refreshListForce",
				payload: {}
			})
		})
	}
}